package com.practice.demo;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/login")
public class logindemo {
    @RequestMapping("/startlogin")
    public boolean startlogin(String userName, String password, HttpSession session){
        //判断是否为null
        if(!StringUtils.hasLength(userName)||!StringUtils.hasLength(password)){
            System.out.println(password+userName);
            return false;
        }
        //校验账号和密码
        //假设现在密码为zhangsan,密码为123456
        if("zhangsan".equals(userName)&&"123456".equals(password)){
            //存储session,以Key,value的形式存储
            session.setAttribute("name","zhangsan");
            return true;
        }
        System.out.println(password+userName);
        return false;
    }
    @RequestMapping("/getUserInfo")
    public String getUserInfo(HttpSession request){
        String username= (String) request.getAttribute("name");
        return "获取到的userName:"+username;
    }
}
